﻿using System;
using System.Globalization;

namespace LsTool
{
    /// <summary>
    /// 获取唯一ID
    /// </summary>
    public class OnlyIdCom
    {


        /// <summary>
        /// The random.
        /// </summary>
        static Random random = new Random();

        /// <summary>
        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks(int x)
        {
            return DateTime.Now.Ticks + RandomStr(x);
        }

        /// <summary>
        /// 唯一时间(yymmdd)Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTime(int x)
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss") + RandomStr(x);
        }

        /// <summary>
        /// 唯一时间(yymmdd)Id
        /// </summary>
        /// <returns>
        /// </returns>
        public static string OnlyTime16()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss") + RandomStr(100);
        }

        /// <summary>
        /// 唯一时间(yymmdd)Id
        /// </summary>
        /// <returns>
        /// </returns>
        public static string OnlyTime20()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssff") + RandomStr(10000);
        }

        /// <summary>
        /// 随机数
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        static string RandomStr(int x)
        {
            int sta = x;
            int end = sta * 10 - 1;
            return random.Next(sta, end).ToString(CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks16()
        {
            return DateTimeCom.GetTimeStamp() + RandomStr(100000);
        } 
        /// <summary>
        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks12()
        {
            return DateTimeCom.GetTimeStamp() + RandomStr(10);
        } 

        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks8()
        {
            return (DateTimeCom.GetTimeStamp(10000)) + RandomStr(10);
        }

        /// 唯一时间戳Id
        /// </summary>
        /// <param name="x">位数，输入10的倍数（如：10,100,1000）</param>
        /// <returns></returns>
        public static string OnlyTicks10()
        {
            return (DateTimeCom.GetTimeStamp(1000)) + RandomStr(100);
        }
    }
}